package com.joinway.admin.service;

import java.util.Map;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.joinway.${module}.bean.domain.${Table};
import com.joinway.appx.bean.view.DataGridView;
import com.joinway.appx.bean.view.TableResultView;
import com.joinway.appx.service.table.CustomTableService;
import com.joinway.appx.service.table.DefaultTableService;
import com.joinway.appx.service.table.TableServiceHelper;

public class ${Table}TableService extends DefaultTableService implements CustomTableService {

	@Override
	public String getTargetTableName() {
		return "${tableName}";
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public DataGridView findRecord(String table, Map<String, String[]> params) throws Exception {
		// TODO 在这里重写默认查询逻辑
		return super.findRecord(table, params);
	}

	@Transactional(rollbackFor=Throwable.class)
	@Override
	public TableResultView saveTable(String table, Map<String, String[]> params) throws Exception {
		// 从输入参数构造要保存的域对象
		${Table} ${table} = TableServiceHelper.buildEntity(params, ${Table}.class);
		
		/*
		 * TODO 保存前可能会做如下操作
		 * 1. 计算并重设字段值
		 * 2. 级联更新或插入
		 */
		
		// 保存数据到对应的表中
		repository.save(${table});
		
		TableResultView view = new TableResultView();
		view.setId(${table}.getId());
		
		return view;
	}

	@Transactional(rollbackFor=Throwable.class)
	@Override
	public TableResultView deleteTable(String table, String softColumn, String[] ids) throws Exception {
		/*
		 * TODO 可能会做如下操作
		 * 1. 级联删除
		 */
		
		return super.deleteTable(table, softColumn, ids);
	}

}
